<?php

require_once 'header.php';
	
try
{
	configLayer::load();
}
catch( PDOException $e )
{
	// means that there is no needed tables.
	if(strpos($e->getMessage(), 'no such table:'))
	{
		$ImportFile = '';
		if(strtolower(configParser::getData('sql_type')) == "sqlite")
		{
			$ImportFile = './sql/SQLite.sql';
		}
		else
		{
			$ImportFile = './sql/MySQL.sql';
		}
		
		$ImportFile = file( $ImportFile );
		$Query = '';
		foreach( $ImportFile as $QueryLine )
		{
				$Query .= $QueryLine;
		}
		
		$Query = explode( ';', $Query );
		
		foreach( $Query as $Q )
		{
			dbLayer::query( $Q . ";" );
		}
		echo 'Database has updated tables to be suitable for our A.A.C. Refresh the page.';
		die();
	}
}

sessionLayer::load();
dataLayer::load();

//Module
viewLayer::$var->loggedIn = sessionLayer::getAccount();
viewLayer::$var->isGM = sessionLayer::isGamemasterAccount();

$Mod = ($_GET['m']) ? $_GET['m'] : 'index';
$Act = ($_GET['a']) ? $_GET['a'] : 'index';

if( file_exists( './mods/'.$Mod.'.php' ) )
{
	require_once './mods/'.$Mod.'.php';
	$n = 'Mod'.$Mod;
	if( class_exists($n) )
	{
		$i = new $n;
		if( method_exists($i, $Act) )
		{
			$i->$Act();
		}
		else
		{
			viewLayer::render(NULL, '404: Not found.');
		}
	}
	else
	{
		viewLayer::render(NULL, '404: Not found.');
	}
}

//End
sessionLayer::save();
viewLayer::output();

?>